/* 
 * Copyright 2014 ShangDao.Ltd  All rights reserved.
 * SiChuan ShangDao.Ltd PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * 
 * @ActSearch.java  2014-9-10 下午3:13:35 - Carson
 * @author Guzhu
 * @email:muyi126@163.com
 * @version 1.0
 */

package com.android.tiango.baba.act;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.scsd.tiango.R;
import com.android.support.httpclient.HttpCallBack;
import com.android.support.httpclient.HttpClientAsync;
import com.android.support.httpclient.HttpParams;
import com.android.tiango.baba.BaBaseActivity;
import com.android.tiango.baba.adapter.SearchAdapter;
import com.android.tiango.baba.cookie.HttpUrl;
import com.android.tiango.baba.entity.SubjectEntity;
import com.android.tiango.baba.entity.SubjectModel;
import com.android.tiango.baba.util.Constants;
import com.android.tiango.baba.util.StringUtils;
import com.android.tiango.baba.util.ToastUtils;
import com.android.tiango.baba.view.PullListView;
import com.android.tiango.baba.view.PullListView.PullListViewListener;

public class ActSearch extends BaBaseActivity implements PullListViewListener, OnClickListener {
	private ImageView mBackImageView;
	private EditText mSearchEditText;
	private PullListView mListView;
	private SearchAdapter adapter;
	private int mPageIndex = 1;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.layout_search);
		mBackImageView = (ImageView) findViewById(R.id.image_back);
		mSearchEditText = (EditText) findViewById(R.id.et_search);
		mListView = (PullListView) findViewById(R.id.id_pull_listview);
		mListView.setPullListener(this);
		adapter = new SearchAdapter(this);
		mListView.setAdapter(adapter);
		mBackImageView.setOnClickListener(this);
		mListView.startOnRefresh();
		sendReply();
	}

	@Override
	public void onPullRefresh() {
		mPageIndex = 1;
		loadData();
	}

	@Override
	public void onPullLoadMore() {
		mPageIndex += 1;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.image_back:
			finish();
			break;

		default:
			break;
		}

	}

	@Override
	public boolean onSearchRequested() {
		loadSearch();
		return true;
	}

	private void loadSearch() {
		String searchMsg = mSearchEditText.getText().toString();
		if (StringUtils.isEmpty(searchMsg)) {
			ToastUtils.showMessage("请输入搜索的内容");
		} else {
			// loadSearch(searchMsg);
		}
	}

	// 获取分类
	private void loadData() {
		HttpClientAsync client = HttpClientAsync.getInstance();
		HttpParams param = new HttpParams();
		param.put("pageIndex", mPageIndex);
		param.put("pageSize", Constants.PAGE_SIZE);
		client.get(HttpUrl.getUrl(HttpUrl.AllSubjects), param, new HttpCallBack() {

			@Override
			public void onHttpSuccess(Object arg0) {
				SubjectEntity entity = (SubjectEntity) arg0;
				ArrayList<SubjectModel> data = entity.getData();

				if (null != data) {
					adapter.putData(data, mPageIndex);
					if (data.size() < 1) {
						ToastUtils.showMessage("暂无数据");
					}
				}

				mListView.onRefreshFinish();
				mListView.onLoadFinish();

			}

			@Override
			public void onHttpStarted() {

			}

			@Override
			public void onHttpFailure(Exception arg0, String arg1) {
				mListView.onRefreshFinish();
				mListView.onLoadFinish();

				if (!StringUtils.isEmpty(arg1)) {
					ToastUtils.showMessage(arg1);
				}
			}
		}, SubjectEntity.class);

	}

	private void sendReply() {
		mSearchEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
			@Override
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				String key = mSearchEditText.getText().toString();
				if (StringUtils.isEmpty(key)) {
					ToastUtils.showMessage("请输入搜索关键词");
				} else {
					Intent intent = new Intent(ActSearch.this, ActPostList.class);
					intent.putExtra(Constants.BARID, key);
					ActSearch.this.startActivity(intent);
				}
				return false;
			}
		});
	}
}
